export const get = (url: string, params = {}, headers?: Record<string, any>) => {
  const queryString = (url.includes('?') ? '&' : '?') + new URLSearchParams(params).toString();

  return fetch(url + queryString, {
    method: 'GET',
    cache: 'no-cache',
    headers: Object.assign({}, headers),
  }).then(response => response.json());
};

export const post = (url: string, params = {}, body: any, headers?: Record<string, any>) => {
  const queryString = (url.includes('?') ? '&' : '?') + new URLSearchParams(params).toString();

  return fetch(url + queryString, {
    method: 'POST',
    headers: Object.assign(
      {
        'Content-Type': 'application/json;charset=utf-8',
      },
      headers
    ),
    body: JSON.stringify(body, (key, val) => {
      if (typeof val === 'function') {
        return val.toString();
      } else {
        return val;
      }
    }),
  }).then(response => response.json());
};
